package dao.impl;

import dao.KnowledgeDao;
import pojo.Knowledge;
import util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wq3stone
 */
public class KnowledgeDaoImpl implements KnowledgeDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    @Override
    public void insert(Knowledge knowledge) {
        try {
            String sql = "insert into t_knowledge (uid,title,category,keyword,content,date) values (?,?,?,?,?,?)";
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,knowledge.getUid());
            ps.setString(2, knowledge.getTitle());
            ps.setString(3, knowledge.getCategory());
            ps.setString(4,knowledge.getKeyword());
            ps.setString(5, knowledge.getContent());
            Date sqlDate = new Date(knowledge.getDate().getTime());
            ps.setDate(6, sqlDate);
            ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("插入失败");
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,null);
        }
    }

    @Override
    public void delete(String key,String value) {
        try {
            String sql = "delete from t_knowledge where " + key + "=?";
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            switch (key) {
                case "id":
                    ps.setInt(1,Integer.parseInt(value));
                    break;
                case "uid":
                    ps.setInt(1,Integer.parseInt(value));
                    break;
                default:
                    ps.setString(1,value);
                    break;
            }
            ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("删除失败");
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,null);
        }
    }

    @Override
    public void update(Knowledge knowledge) {
        try {
            String sql = "update t_knowledge set title=?,category=?,keyword=?,content=?,date=? where id=?";
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1,knowledge.getTitle());
            ps.setString(2, knowledge.getCategory());
            ps.setString(3, knowledge.getKeyword());
            ps.setString(4, knowledge.getContent());
            Date sqlDate = new Date(knowledge.getDate().getTime());
            ps.setDate(5,sqlDate);
            ps.setInt(6,knowledge.getId());
            ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("更新失败");
            e.printStackTrace();
        }
    }

    @Override
    public List<Knowledge> query(String key, String value) {
        List<Knowledge> list = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            if(key.equals("id")||key.equals("uid")){
                String sql = "select * from t_knowledge where " + key + "=?";
                ps = conn.prepareStatement(sql);
                ps.setInt(1,Integer.parseInt(value));
            }else{
                String sql = "select * from t_knowledge where " + key + " like ?";
                ps = conn.prepareStatement(sql);
                ps.setString(1,"%"+value+"%");
            }
            rs = ps.executeQuery();
            while(rs.next()){
                Knowledge knowledge = new Knowledge();
                knowledge.setId(rs.getInt("id"));
                knowledge.setUid(rs.getInt("uid"));
                knowledge.setTitle(rs.getString("title"));
                knowledge.setCategory(rs.getString("category"));
                knowledge.setKeyword(rs.getString("keyword"));
                knowledge.setContent(rs.getString("content"));
                knowledge.setDate(rs.getDate("date"));
                list.add(knowledge);
            }
        } catch (SQLException e) {
            System.out.println("查询失败");
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,null);
        }
        return list;
    }

    @Override
    public List<Knowledge> query(String key, String value, int uid) {
        List<Knowledge> list = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from t_knowledge where " + key + " like ? and uid=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,"%"+value+"%");
            ps.setInt(2,uid);
            rs = ps.executeQuery();
            while(rs.next()){
                Knowledge knowledge = new Knowledge();
                knowledge.setId(rs.getInt("id"));
                knowledge.setUid(rs.getInt("uid"));
                knowledge.setTitle(rs.getString("title"));
                knowledge.setCategory(rs.getString("category"));
                knowledge.setKeyword(rs.getString("keyword"));
                knowledge.setContent(rs.getString("content"));
                knowledge.setDate(rs.getDate("date"));
                list.add(knowledge);
            }
        } catch (SQLException e) {
            System.out.println("查询失败");
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,ps,null);
        }
        return list;
    }
}
